package com.pro.system.Controller;

import com.pro.system.Repository.CommentRepository;
import com.pro.system.Repository.MovieDetailRepository;
import com.pro.system.Repository.MovieRepository;
import com.pro.system.Repository.UserRepository;
import com.pro.system.dto.UserComment;
import com.pro.system.entity.Comment;
import com.pro.system.entity.Movie;
import com.pro.system.entity.MovieDetail;
import com.pro.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Controller
@RequestMapping
public class MainController {

    @Autowired
    MovieRepository movieRepository;

    @Autowired
    MovieDetailRepository movieDetailRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    CommentRepository commentRepository;

    @GetMapping("admin")
    public String displayUserForm(){
        return "web/background/index";
    }

    @GetMapping
    public String displayFgForm(Model model){
        List<Movie> orderByHot = movieRepository.findOrderByHot(6);
        model.addAttribute("movie",orderByHot);
        return "web/foreground/index";
    }

    @PostMapping()
    public String fgsearch(Model model,@RequestParam(name = "fgsearchinput") String fgsearchinput){
        if(fgsearchinput ==null || fgsearchinput==""){
            return "redirect:/";
        }
        else {
            List<Movie> byNameLike = movieRepository.findByNameLike(fgsearchinput);
            model.addAttribute("movie1",byNameLike);

            List<Movie> orderByHot = movieRepository.findOrderByHot(6);
            model.addAttribute("movie",orderByHot);
            return "web/foreground/index";
        }
    }

    @GetMapping("index-after")
    public String displayAfterFgForm(Model model, Principal principal){
        User byName = userRepository.findByName(principal.getName());
        List<Movie> orderByHot = movieRepository.findOrderByHot(6);
        model.addAttribute("movie",orderByHot);
        model.addAttribute("user",byName);
        return "web/foreground/index-after";
    }

    @GetMapping("fgdetail/{id}")
    public String displaydetailFgForm(Model model,@PathVariable("id") Integer id,Principal principal, String fgcontent){
        Movie byId = movieRepository.findByid(id);
        model.addAttribute("movie",byId);

        MovieDetail byid = movieDetailRepository.findByid(id);
        model.addAttribute("movieDetail",byid);

        List<Movie> all = movieRepository.findAll();
        model.addAttribute("movie1",all);

        List<UserComment> byMovieId = commentRepository.findByMovieId(id);
        model.addAttribute("comment1",byMovieId);

        if(principal == null){
            model.addAttribute("atler","禁止非用户评论！");
        }
        return "web/foreground/fgdetail";
    }

    @PostMapping("fgdetail/{id}")
    public String commentFgForm(Model model,@PathVariable("id") Integer id,Principal principal, String fgcontent){
        Movie byId = movieRepository.findByid(id);
        model.addAttribute("movie",byId);

        if(principal!=null){
                User byName = userRepository.findByName(principal.getName());
                Comment comment = new Comment();
                comment.setMovie1(byId);
                comment.setUser(byName);
                comment.setContent(fgcontent);
                commentRepository.save(comment);
        }
        return "redirect:/fgdetail/{id} ";
    }

}
